package com.bp.scoringgun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bp.scoringgun.common.R;
import com.bp.scoringgun.entity.User;
import com.bp.scoringgun.service.DataParserService;
import com.bp.scoringgun.service.UserService;
import com.bp.scoringgun.service.impl.DataParserServiceImpl;
import com.bp.scoringgun.service.impl.DataStatServiceImpl;
import com.bp.scoringgun.service.impl.PredictionServiceImpl;
import com.bp.scoringgun.utils.GitHubArchiveDownloader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@RequestMapping("/ranking")
public class RankingController {

    @Autowired
    private GitHubArchiveDownloader gitHubArchiveDownloader;

    @Autowired
    private DataParserServiceImpl dataParserService;

    @Autowired
    private PredictionServiceImpl predictionService;

    @Autowired
    private DataStatServiceImpl dataStatService;

    @Value("${scoring-gun.path}")
    private String dataPath ;

    @Autowired
    private UserService userService;




    @GetMapping("/page")
    public R<Page<User>> page(int page, int pageSize, String nation, String field) {
        Page<User> pageInfo = new Page<>(page, pageSize);

        // 创建查询条件构造器
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 如果传递了 nation 参数，添加匹配条件
        if (nation != null && !nation.isEmpty()) {
            lambdaQueryWrapper.eq(User::getNation, nation);
        }

        // 如果传递了 field 参数，添加模糊匹配条件
        if (field != null && !field.isEmpty()) {
            lambdaQueryWrapper.like(User::getField, field);
        }

        // 按照 userRank 从高到低排序
        lambdaQueryWrapper.orderByAsc(User::getUserRank);

        // 执行分页查询
        userService.page(pageInfo, lambdaQueryWrapper);

        // 返回查询结果
        return R.success(pageInfo);
    }



    @GetMapping("/add-data")
    public R<String> add(String date, String time){
        //下载阶段
        gitHubArchiveDownloader.downloadAndDecompress(date, Integer.parseInt(time));
        String filePath = dataPath + "\\" + date + "-" + time + ".json";
        String message = dataParserService.parseJsonFile(filePath);
        dataStatService.calculateProjectScores();
        dataStatService.calculateAndNormalizeUserScores();
        if(message.equals("success")){
            return R.success("添加数据并计算分数成功");
        }
        else{
            return R.error(message);
        }
    }

    @GetMapping("/guess-nations")
    public R<String> guess(){
        String message = dataParserService.writeLocationAndLanguage();
        dataParserService.clearMaps();
        predictionService.predictNation();
        if (message.equals("success")){
            return R.success("获取地区和领域并推测未知地区成功！");
        }
        else{
            return R.error(message);
        }
    }
}
